// 深拷贝
const deepClone = (obj, hash = new WeakMap()) => {
	// 如果传入的是值类型/null，直接返回
	if (typeof obj !== 'object' || obj === null) return obj;

	// 处理过的值直接返回-处理循环引用
	if (hash.get(obj)) return hash.get(obj);

	let result = obj instanceof Array ? [] : {};
	hash.set(obj, result);
	for (const key in obj) {
		if (Object.hasOwnProperty.call(obj, key)) {
			result[key] = deepClone(obj[key], hash);
		}
	}
	return result;
};
/**
 * 生产随机字符串
 * @param {String} len 生成的字符串长度
 * @returns {String}
 */
const randomString = (len = 10) => {
	const $chars =
		'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789' /** **默认去掉了容易混淆的字符oOLl,9gq,Vv,Uu,I1*** */,
		maxPos = $chars.length;
	let pwd = '';
	for (let i = 0; i < len; i++) {
		pwd += $chars.charAt(Math.floor(Math.random() * maxPos));
	}
	return pwd;
};

/**
 * 返回js数据类型
 * @param {*} obj
 * @returns {String} js数据类型
 */
const getDataType = obj => {
	// tostring会返回对应不同的标签的构造函数
	const { toString } = Object.prototype;
	const map = {
		'[object Boolean]': 'boolean',
		'[object Number]': 'number',
		'[object String]': 'string',
		'[object Function]': 'function',
		'[object Array]': 'array',
		'[object Date]': 'date',
		'[object RegExp]': 'regExp',
		'[object Undefined]': 'undefined',
		'[object Null]': 'null',
		'[object Object]': 'object'
	};
	if (obj instanceof Element) {
		return 'element';
	}
	return map[toString.call(obj)];
};

exports.deepClone = deepClone
exports.randomString = randomString
exports.getDataType = getDataType